﻿using System;
using System.Linq;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

namespace GlimpseWcf.Server
{
    [AttributeUsage(AttributeTargets.Class)]
    public class WcfServiceBehavior : Attribute, IServiceBehavior
    {
        public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
            //throw new System.NotImplementedException();
        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
            foreach (var eDispatcher in serviceHostBase.ChannelDispatchers.Cast<ChannelDispatcher>().SelectMany(cDispatcher => cDispatcher.Endpoints))
            {
                eDispatcher.DispatchRuntime.MessageInspectors.Add(new WcfDispatchMessageInspector());
            }
        }

        public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
            //throw new System.NotImplementedException();
        }
    }
}
